package com.xm.web.config;

import com.xm.service.realm.ShiroRealm;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {
    @Bean
    public Realm realm(){
        return new ShiroRealm();
    }

    @Bean
    public ShiroFilterChainDefinition chainDefinition(){
        DefaultShiroFilterChainDefinition chainDefinition
                =new DefaultShiroFilterChainDefinition();
        chainDefinition.addPathDefinition("/user/logout","logout");
        chainDefinition.addPathDefinition("/manage/**","user");
        chainDefinition.addPathDefinition("/buy/**","user");
        return chainDefinition;
    }

    @Bean
    protected CacheManager shiroCacheManager() {
        return new MemoryConstrainedCacheManager();
    }

    //解决controller类上加权限注解时mapping注解失效
    @Bean
    public DefaultAdvisorAutoProxyCreator deefaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator creator=
                new DefaultAdvisorAutoProxyCreator();
        creator.setProxyTargetClass(true);
        return creator;
    }
}
